package com.jit.monitor.ui.adapter;

import android.support.annotation.Nullable;
import android.widget.CheckBox;
import android.widget.CompoundButton;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jit.monitor.R;
import com.jit.monitor.model.bean.AlarmRangeBean;

import java.util.List;

/**
 * @author crazyZhangxl on 2019/1/16.
 * Describe:
 */
public class AlarmRangAdapter extends BaseQuickAdapter<AlarmRangeBean,BaseViewHolder> {
    private boolean onBind;
    private CheckBoxChangedListener mCheckBoxChangedListener;
    public AlarmRangAdapter(int layoutResId, @Nullable List<AlarmRangeBean> data,CheckBoxChangedListener checkBoxChangedListener) {
        super(layoutResId, data);
        this.mCheckBoxChangedListener = checkBoxChangedListener;
    }

    @Override
    protected void convert(BaseViewHolder helper, AlarmRangeBean item) {
        CheckBox checkBox =  helper.getView(R.id.checkBox);
        onBind = true;
        checkBox.setChecked(item.isState());
        onBind = false;
        // 设置文本
        helper.setText(R.id.tvName,item.getName());

        checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (mCheckBoxChangedListener != null && !onBind){
                    mCheckBoxChangedListener.onCheckBoxChanged(helper.getAdapterPosition(),isChecked);
                }
            }
        });
    }

    public interface CheckBoxChangedListener{
        void onCheckBoxChanged(int position,boolean state);
    }
}
